﻿using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

namespace GUITree
{
	// Token: 0x020004B4 RID: 1204
	internal class ObjectPool<T> where T : new()
	{
		// Token: 0x06001E20 RID: 7712 RVA: 0x000BEC1C File Offset: 0x000BCE1C
		public ObjectPool(UnityAction<T> actionOnGet, UnityAction<T> actionOnRelease)
		{
			this.m_ActionOnGet = actionOnGet;
			this.m_ActionOnRelease = actionOnRelease;
		}

		// Token: 0x17000339 RID: 825
		// (get) Token: 0x06001E21 RID: 7713 RVA: 0x000BEC40 File Offset: 0x000BCE40
		// (set) Token: 0x06001E22 RID: 7714 RVA: 0x000BEC48 File Offset: 0x000BCE48
		public int countAll { get; private set; }

		// Token: 0x1700033A RID: 826
		// (get) Token: 0x06001E23 RID: 7715 RVA: 0x000BEC54 File Offset: 0x000BCE54
		public int countActive
		{
			get
			{
				return this.countAll - this.countInactive;
			}
		}

		// Token: 0x1700033B RID: 827
		// (get) Token: 0x06001E24 RID: 7716 RVA: 0x000BEC64 File Offset: 0x000BCE64
		public int countInactive
		{
			get
			{
				return this.m_Stack.Count;
			}
		}

		// Token: 0x06001E25 RID: 7717 RVA: 0x000BEC74 File Offset: 0x000BCE74
		public T Get()
		{
			T t;
			if (this.m_Stack.Count == 0)
			{
				t = Activator.CreateInstance<T>();
				this.countAll++;
			}
			else
			{
				t = this.m_Stack.Pop();
			}
			if (this.m_ActionOnGet != null)
			{
				this.m_ActionOnGet(t);
			}
			return t;
		}

		// Token: 0x06001E26 RID: 7718 RVA: 0x000BECD0 File Offset: 0x000BCED0
		public void Release(T element)
		{
			if (this.m_Stack.Count > 0 && object.ReferenceEquals(this.m_Stack.Peek(), element))
			{
				GlobalData.Debug_LogError("Internal error. Trying to destroy object that is already released to pool.");
			}
			if (this.m_ActionOnRelease != null)
			{
				this.m_ActionOnRelease(element);
			}
			this.m_Stack.Push(element);
		}

		// Token: 0x0400208A RID: 8330
		private readonly Stack<T> m_Stack = new Stack<T>();

		// Token: 0x0400208B RID: 8331
		private readonly UnityAction<T> m_ActionOnGet;

		// Token: 0x0400208C RID: 8332
		private readonly UnityAction<T> m_ActionOnRelease;
	}
}
